package com.basis.utils;

import org.codehaus.jettison.json.JSONObject;
import org.openbravo.client.kernel.BaseActionHandler;
import org.openbravo.client.kernel.RequestContext;
import org.openbravo.service.json.JsonConstants;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

public abstract class BaseServletHandler extends BaseActionHandler{
	@Override
	public void execute() {
		try {
			final HttpServletRequest request = RequestContext.get().getRequest();
			request.setCharacterEncoding("utf-8");  //设置编码
		  	
			final HttpServletResponse response = RequestContext.get().getResponse();
			final JSONObject result = service(request, response);
		  	response.setContentType(JsonConstants.JSON_CONTENT_TYPE);
		  	response.setHeader("Content-Type", JsonConstants.JSON_CONTENT_TYPE);
			response.getWriter().write(result.toString());
		} catch (Exception e) {
			throw new IllegalStateException(e);
		}
	}

	protected abstract JSONObject service(HttpServletRequest request,HttpServletResponse response);
	
	@Override
	protected JSONObject execute(Map<String, Object> parameters, String content) {
		// TODO Auto-generated method stub
		return null;
	}
}
